﻿Imports System.Reflection
Imports System.Text
Imports Kairyu.Extension

Namespace Load

    ''' <summary>
    ''' 関連テーブルエイリアス
    ''' </summary>
    ''' <remarks></remarks>
    Public Class RelationTableAlias
        Inherits TableAlias

        ''' <summary>
        ''' Nullを許容するかどうかを指定します
        ''' </summary>
        ''' <param name="value"></param>
        ''' <remarks></remarks>
        Friend Sub SetIsNullable(value As Boolean)
            _IsNullable = value
        End Sub

        Private _IsNullable As Boolean
        ''' <summary>
        ''' Nullを許容するかを示します
        ''' </summary>
        Overrides ReadOnly Property IsNullable As Boolean
            Get
                Return _IsNullable
            End Get
        End Property

        ''' <summary>
        ''' ルートかどうかを示します
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides ReadOnly Property IsRoot As Boolean
            Get
                Return False
            End Get
        End Property

        ''' <summary>
        ''' 関連元テーブルエイリアスとの結合条件を示します
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Relations As New List(Of IParentRelation)

        ''' <summary>
        ''' SELECT句の取得
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides Function GetSelectText() As String
            Dim s As String = String.Empty

            '自身の列
            For Each item In (From el In Me.ColumnAliases Where el.IsSelectable AndAlso el.BaseColumnAlias Is Nothing)
                If String.IsNullOrEmpty(s) = False Then s &= ", "
                s &= item.SelectCommandText
            Next

            '上層へカスケード
            For Each item In Me.RelationTableAliases
                If String.IsNullOrEmpty(s) = False Then s &= ", "
                s &= item.GetSelectText
            Next

            Return s
        End Function

        ''' <summary>
        ''' FROM句の取得
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides Function GetFromText() As String
            '自身のテーブル
            Dim j As String = String.Format(" {0} {1} {2} ON", If(Me.IsNullable, "LEFT JOIN", "INNER JOIN"), Me.LoadTableFullName, Me.TableAliasFullName)

            Dim c As New StringBuilder
            For Each item In Me.Relations
                c.AppendDelimiter(" AND ")
                c.AppendFormat("{0}.{1} = {2}.{3}", Me.BaseTableAlias.TableAliasFullName, item.MyColumnName, Me.TableAliasFullName, item.YourColumnName)
            Next

            '上層へカスケード
            Dim s As New StringBuilder
            For Each item In Me.RelationTableAliases
                s.Append(item.GetFromText)
            Next

            Return String.Format("{0} {1}{2}", j, c.ToString, s.ToString)
        End Function

    End Class

End Namespace
